package self.wargon.springcloudstu.servicea.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.easydev.base.webbase.bo.JsonEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import self.wargon.springcloudstu.common.bo.UserInfo;
import self.wargon.springcloudstu.servicea.client.UserServiceClient;
import self.wargon.springcloudstu.servicea.service.TestService;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/api/test")
public class TestController {

    @Autowired
    TestService testService;

    @Operation(summary = "测试获取用户信息")
    @GetMapping("/userInfo/userId/{userId}")
    public JsonEntity<UserInfo> getUserInfo(@PathVariable("userId") String userId) throws ExecutionException, InterruptedException {
//        return JsonEntity.of(testService.getUserInfo(userId));
        CompletableFuture<UserInfo> future = testService.asyncGetUserInfo(userId);
        return JsonEntity.of(future.get());
    }


    @Operation(summary = "测试")
    @GetMapping("/test")
    public JsonEntity<String> test() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(1);

        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("任务执行中...");
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                System.out.println("任务被中断！");
                Thread.currentThread().interrupt(); // 可选：保留中断状态
            }
        }, executor);

        // 模拟中断
        Thread.sleep(3000);
        future.cancel(true); // 标记取消，但不会真正中断线程

        executor.shutdown(); // 这才是触发线程中断的关键


        return JsonEntity.of("任务已取消");
    }
}
